from typing import Dict, Optional, List, Set, Tuple
from fastapi import FastAPI, Path, Query, Body
import uvicorn
from pydantic import BaseModel, Field

app = FastAPI()

'''
    带有类型参数的字段
    Python 有一种特定的方法来声明具有内部类型或类型参数的列表
    
    其实前面都见过，就是
    List[str]
    Set[str]
    Tuple[str]
    Dict[str, int]
    
    
    在 Pydantic Model 中使用 typing 提供的类型
'''


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: List[str] = []
    address: Set[str] = set()
    phone: Tuple[int] = ()
    ext: Dict[str, str] = {}


item = Item(name="lxq", price=12.2)
print(item.dict())

print("=======================")

# Pydantic 嵌套模型
# 模型 1
class Foo(BaseModel):
    count: int
    size: float = None


# 模型 2
class Bar(BaseModel):
    apple = 'x'
    banana = 'y'

# 模型 3
# 模型 3
class Spam(BaseModel):
   # 字段类型是 Pydantic Model，这就是嵌套模型
    foo: Foo
    bars: List[Bar]


f = Foo(count=2)
b = Bar()
s = Spam(foo=f, bars=[b])

print(s.dict())